package com.etc.togo.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.etc.togo.dao.ICarDAO;
import com.etc.togo.domain.Car;
@Repository
@Transactional
public class CarDAOImpl extends BaseDAOImpl<Car> implements ICarDAO{

	Logger logger=LoggerFactory.getLogger(CarDAOImpl.class);
	@SuppressWarnings("unchecked")
	@Override
	public List<Car> pageCar(int pageNum, int pageSize,String status) {
		// TODO Auto-generated method stub
		String hql="from Car c where c.status = :status";
		Query query = getSession().createQuery(hql);
		query.setParameter("status", status);
		query.setFirstResult((pageNum-1)*pageSize);
		query.setMaxResults(pageSize);
		List<Car> carList=query.list();
		if (carList.size()>0)
			logger.info("carList size > 0");
		return carList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Car> findByTopic(String topic,int pageNum,int pageSize) {
		// TODO Auto-generated method stub
		String hql="from Car c where c.topic like :topic and c.status ='1'";
		Query query = getSession().createQuery(hql);
		query.setParameter("topic", "%"+topic+"%");
		logger.info("pageNum "+pageNum);
		query.setFirstResult((pageNum-1)*pageSize);
		query.setMaxResults(pageSize);
		List<Car> carList=query.list();
		if (carList.size()>0)
			logger.info("carList size > 0");
		return carList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Car> findByCategory(String category, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		String hql="from Car c where c.category =:category and c.status = '1' ";
		Query query = getSession().createQuery(hql);
		query.setParameter("category", category);
		query.setFirstResult((pageNum-1)*pageSize);
		query.setMaxResults(pageSize);
		List<Car> carList=query.list();
		return carList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Car> findByModel(String model,int pageNum,int pageSize) {
		String hql="from Car c where c.model =:model and c.status = '1' ";
		Query query = getSession().createQuery(hql);
		query.setParameter("model", model);
		query.setFirstResult((pageNum-1)*pageSize);
		query.setMaxResults(pageSize);
		List<Car> carList=query.list();
		return carList;
	}

	@Override
	public List<Car> findByUser(Long owner, int pageNum, int pageSize) {
		String hql="from Car c where c.owenr =:owner and c.status = '1' ";
		Query query = getSession().createQuery(hql);
		query.setParameter("owner", owner);
		query.setFirstResult((pageNum-1)*pageSize);
		query.setMaxResults(pageSize);
		List<Car> carList=query.list();
		return carList;
	}

}
